package top.bugger168.content.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.bugger168.base.exception.ValidationGroups;
import top.bugger168.base.model.PageParams;
import top.bugger168.base.model.PageResult;
import top.bugger168.content.model.dto.AddCourseDto;
import top.bugger168.content.model.dto.CourseBaseInfoDto;
import top.bugger168.content.model.dto.EditCourseDto;
import top.bugger168.content.model.dto.QueryCourseParamsDto;
import top.bugger168.content.model.po.CourseBase;
import top.bugger168.content.service.CourseBaseInfoService;
import top.bugger168.content.util.SecurityUtil;

import javax.annotation.Resource;

/**
 * 课程信息编辑接口
 *
 * @author wanghaidong
 * @date 2023/08/28
 */
@Api(value = "课程信息编辑接口",tags = "课程信息编辑接口")
@RestController
public class CourseBaseInfoController {
    @Resource
    CourseBaseInfoService courseBaseInfoService;

    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAnyAuthority('xc_teachmanager_course_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody QueryCourseParamsDto queryCourseParams){
        // 获取当前登录用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        // 用户当前所属机构id
        Long companyId = null;
        if(StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        return courseBaseInfoService.queryCourseBaseList(companyId,pageParams,queryCourseParams);
    }

    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated({ValidationGroups.Insert.class}) AddCourseDto addCourseDto){
        // 参数中的Validated注解是打开参数校验,按照类定义中的相应注解校验,校验出错会抛MethodArgumentNotValidException异常
        // TODO: 2023/4/10 目前认证系统没有上线机构id硬编码
        Long companyId = 1688888888L;
        return courseBaseInfoService.createCourseBase(companyId,addCourseDto);
    }

    @ApiOperation("根据id查询课程基础信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        // 取出当前用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user);
        return courseBaseInfoService.getCourseBaseInfo(courseId);
    }

    @ApiOperation("修改课程基础信息")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated({ValidationGroups.Update.class}) EditCourseDto editCourseDto){
        // TODO: 2023/4/10 目前认证系统没有上线机构id硬编码
        Long companyId = 1688888888L;
        return courseBaseInfoService.updateCourseBase(companyId,editCourseDto);
    }

    @ApiOperation("删除整个课程")
    @DeleteMapping("/course/{courseId}")
    public void removeCourse(@PathVariable Long courseId){
        courseBaseInfoService.removeCourse(courseId);
    }
}